package com.kmbeast.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kmbeast.context.LocalThreadHolder;
import com.kmbeast.mapper.AreaMapper;
import com.kmbeast.mapper.UserAreaMapper;
import com.kmbeast.pojo.api.ApiResult;
import com.kmbeast.pojo.api.Result;
import com.kmbeast.pojo.dto.AreaQueryDto;
import com.kmbeast.pojo.dto.UserAreaQueryDto;
import com.kmbeast.pojo.entity.Area;
import com.kmbeast.pojo.entity.UserArea;
import com.kmbeast.service.AreaService;
import com.kmbeast.service.UserAreaService;
import com.kmbeast.utils.AssertUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/*
* 用户查询居住地业务逻辑接口
* */
@Service
public class AreaServiceImpl extends ServiceImpl<AreaMapper, Area> implements AreaService {
    /*
    * 用户地区查询
    * */
    @Override
    public Result<List<Area>> list(AreaQueryDto areaQueryDto) {
        List<Area> list = this.baseMapper.list(areaQueryDto);
        Integer count = this.baseMapper.listCount(areaQueryDto);
        return ApiResult.success(list, count);
    }
    /*
     * 用户地区修改
     * */
    @Override
    public Result<String> update(Area area) {
        parenDeal( area);
        updateById(area);
        return ApiResult.success("地区地绑定成功");
    }
    private void parenDeal(Area area) {
        AssertUtils.hasText(area.getName(),"地区名不能为空");
        AreaQueryDto areaQueryDto = new AreaQueryDto();
        areaQueryDto.setName(area.getName());
        Integer count = this.baseMapper.listCount(areaQueryDto);
        AssertUtils.isTrue(count == 0, "该地区不能为空");
    }
    /*
     * 用户地区新增
     * */
    @Override
    public Result<String> saveEntity(Area area) {
        parenDeal(area);
        save(area);
        return ApiResult.success("地区地新增成功");
    }
}
